<template>
    <div>
        <div id="container"></div>
        <div class="input-card">
            <h4>左击获取经纬度：</h4>

        </div>
        <div id="container" class="map"></div>
        <div class="input-card">
            <h4>左击获取经纬度：</h4>
            <div class="input-item">
                <input type="text" readonly="true" id="lnglat">
            </div>
        </div>
    </div>

</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;

onMounted(() => {
    window._AMapSecurityConfig = {
        securityJsCode: "829ad09ae782570684d61f67a080c944",
    };
    AMapLoader.load({
        key: "b196cc6ae3600c969bd4ab4e5f434475", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
    })
        .then((AMap) => {
            map = new AMap.Map("container", {
                zoom: 11,
            });
            //为地图注册click事件获取鼠标点击出的经纬度坐标
            map.on('click', function (e) {
                document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat()
            });
        })
        .catch((e) => {
            console.log(e);
        });
});

onUnmounted(() => {
    map?.destroy();
});
</script>

<style lang="scss" scoped>
#container {
    width: 800px;
    height: 800px;
}
</style>